// vue给开发者暴露出来的增量去配置webpack有文件
// 修改此文件，一定要重启项目
let { resolve } = require('path')

module.exports = {

  // 代理打包生产环境中将不存在，需要通过nodejs来完成
  devServer: {
    // 代理 解决开发时的跨域
    proxy: {
      // 请求地址中pathname的匹配规则
      '/api': {
        // 目标地址
        target: 'https://api.iynn.cn/film',
        // 修改代理时 请求头信息中的host为真实的代理主机名称
        changeOrigin: true,
        // 地址的重写,匹配地址路径和真实的路径有出入
        /* pathRewrite: {
          '^/aaa': ''
        } */
      },
      '/apollo': {
        target: 'https://i.maoyan.com',
        changeOrigin: true
      },
      '/ajax': {
        target: 'https://m.maoyan.com',
        changeOrigin: true
      },
      '/fetch': {
        target: 'http://localhost:9090',
        changeOrigin: true
      }
    }
  },
  // 打包时给vue工程进行代码拆分成模块 -- 不破坏原的vue中的webpack配置，去增量修改对应的配置
  chainWebpack(config) {
    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          // 代码分割
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk('single')
        }
      )
  }


}


